import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
declare var Viewer: any;
@Component({
	selector: 'sa-advise-log',
	templateUrl: './advise-log.component.html',
	styleUrls: ['./advise-log.css']
})
export class AdviseLogComponent implements OnInit {
	tableData:any[] = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	tableParam:any = {
		feedbackPhone:'',
		searchKey: '',
		startTime: '',
		endTime: '',
		state: ''
	}
	isViewer:boolean = false; //默认隐藏
	isShowDelete=false; // 默认不显示删除按钮
	title:any = "";  // 展示回复的标题
	content:any = "";  // 展示回复的内容
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute ) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage||1;
		});
	}
	ngOnInit() {
		this.initBtn();
		this.load();
		System.import('assets/js/viewer/viewer.js').then(
			()=>{System.import('assets/js/viewer/bootstrap.bundle.min.js').then(
				()=>{System.import('assets/js/viewer/viewer.css').then(()=>{
				})
			})
		})
	}
	//初始化权限按钮
	initBtn() {
		var btnData = JSON.parse(decodeURIComponent(localStorage.getItem('cargpsw_wx:bind:adviseLog')));
		if(!btnData) {
			return
		};
		for(let item of btnData) {
			if(item.sourceKey === "wx-bind:advise-log:deleteBtn" && item.parentId) {
				this.isShowDelete = true;
			}
		}
	}
	//table刷新
    refresh() {
    	this.load();
    }
	//表格搜索
	tableSearch() {
		this.curPage = 1;
		this.load();
	}
    //分页回调
    paginate(e){
        this.curPage = e.page + 1;
        this.pageSize = e.rows;
        this.load();
    }
	//初始化加载list数据
	load(){
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
                feedbackPhone: this.tableParam.feedbackPhone,
				searchKey:common.stringTrim(this.tableParam.searchKey),
				startTime: this.tableParam.startTime,
				endTime: this.tableParam.endTime,
				state: this.tableParam.state,
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("appointmentRepairCar/feedbackList.html", reqdata).subscribe(data => {
			if(data.isSuccess){
				let list = data.exhibitDatas;
				for (let index = 0; index < list.length; index++) {
					const item = list[index];
					console.log(item.suggest.length)
					if(item.suggest.length>30){
						item.suggestStr = item.suggest.substring(0,30)+"..."
						item.suggestBtn = true;
					}else{
						item.suggestStr =item.suggest
						item.suggestBtn = false;
					}
					if(item.replyContent.length>30){
						item.replyContentStr = item.replyContent.substring(0,30)+"..."
						item.replyBtn = true;
					}else{
						item.replyContentStr =item.replyContent
						item.replyBtn = false;
					}
				}
				this.tableData = data.exhibitDatas;
				this.totalCount = data.recordCount;
			}else{
				this.jsonApiService.message(data.message);
			}
		})
	}
	imgInfo: any=[];
	//查看照片
	open_img(row,val){
		var that = this;

		var localImgList= [
			"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1595412200017&di=9e0f5a4c276c705cb214d639a579fe98&imgtype=0&src=http%3A%2F%2Fimg.mp.itc.cn%2Fupload%2F20170324%2F0ec5672ee30d4a26b520eeba402f51c5_th.jpeg",
			"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1595412230911&di=106b794a3e5a83781313f23ca5fe1d4c&imgtype=0&src=http%3A%2F%2Ft7.baidu.com%2Fit%2Fu%3D3616242789%2C1098670747%26fm%3D79%26app%3D86%26f%3DJPEG%3Fw%3D900%26h%3D1350",
			"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1595412230911&di=d006f1a41c201446571275399c8cd694&imgtype=0&src=http%3A%2F%2Ft8.baidu.com%2Fit%2Fu%3D1484500186%2C1503043093%26fm%3D79%26app%3D86%26f%3DJPEG%3Fw%3D1280%26h%3D853",
			"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1595412230911&di=2c163e9b56b6f67255349b613c817571&imgtype=0&src=http%3A%2F%2Ft7.baidu.com%2Fit%2Fu%3D3204887199%2C3790688592%26fm%3D79%26app%3D86%26f%3DJPEG%3Fw%3D4610%26h%3D2968",
			"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1595412230911&di=fa55728b61db0e9f2f658bcdb0315ccb&imgtype=0&src=http%3A%2F%2Ft9.baidu.com%2Fit%2Fu%3D4169540006%2C4220376401%26fm%3D79%26app%3D86%26f%3DJPEG%3Fw%3D1000%26h%3D1500",
			"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1595412289242&di=eee47f74e0a7101d31fcbf5e1a6ad704&imgtype=0&src=http%3A%2F%2Ft8.baidu.com%2Fit%2Fu%3D3398851942%2C2332140527%26fm%3D79%26app%3D86%26f%3DJPEG%3Fw%3D1000%26h%3D667",
			"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1595412312827&di=fd1a9a3907592d84b4546821d0df6dd4&imgtype=0&src=http%3A%2F%2Fdiy.qqjay.com%2Fu2%2F2013%2F0115%2Fdec03e7ee48acf5c2c782f1f044b952e.jpg",
			"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1595412312826&di=7678534f29f9845c343c11aa005ee54b&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fforum%2Fw%3D580%2Fsign%3Da9714efaaf86c91708035231f93c70c6%2Fddd3ab59d109b3dea0394e6ac4bf6c81810a4c48.jpg",
			"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1595412312826&di=d10d176349c59fb4df3eb39ae9914852&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201410%2F09%2F20141009224754_AswrQ.jpeg",
			"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2141623099,2896788564&fm=26&gp=0.jpg",
		]
		val.show();
		var imgList = row.imageInfo;
		// var imgList = localImgList;
		this.imgInfo = [];
		for(var b=0;b<imgList.length;b++){
			
			// let localImg;
			// if(imgList[b][0] == '/'){
			// 	localImg = config.BaseURL + imgList[b].substr(1);
			// }else{
			// 	localImg = config.BaseURL + imgList[b];
			// }
			let localImg;
			if(imgList[b].indexOf('http') == -1){
				if(imgList[b][0] == '/'){
					localImg = config.BaseURL + imgList[b].substr(1);
				}else{
					localImg = config.BaseURL + imgList[b];
				}
			}else{
				localImg = imgList[b];
			}

			this.imgInfo.push(localImg);
			// this.imgInfo.push(imgList[b]);
		}
		if(this.isViewer && $(".viewer-container").length >0){
			this.viewer.destroy();
		}
	}
	imgClose(modal){
		modal.hide();
	}
	bigImg:any=''// 放大的图片
	//图片放大
	bigimg(modal,row){
		// modal.show();
		this.bigImg = row
		if(this.isViewer && $(".viewer-container").length >0){
			System.import('assets/js/viewer/viewer.js').then(
				()=>{System.import('assets/js/viewer/bootstrap.bundle.min.js').then(
					()=>{System.import('assets/js/viewer/viewer.css').then(()=>{
						this.showImg();
					})
				})
			})
		}else{
			this.showImg();
		}

	}
	//显示图片组件
	viewer: any='';
	showImg(){
		var that = this;
		var imgList = $(".imagesInfo")[0];
		this.viewer = new Viewer(imgList, {
			url: 'src',
			title: function (image) {
				return image.alt + ' (' + (this.index + 1) + '/' + this.length + ')' ; 
			},
		});
		that.isViewer = true;
	}
	CloseBigImg(modal){
		modal.hide();
	}
	// 删除登录日志
	deleteRow(row) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(() => {
			let parameters = {
				ids: row.id,
				type: "4",
			}
			this.jsonApiService.http_obj("appointmentRepairCar/batchDeleted.html", parameters).subscribe(data => {
				this.jsonApiService.message(data.message);
				this.load();
			})
		})
	}


	// 接收子组件传递过来的数据 
	getSelectTableTime(time) {
		this.tableParam.startTime = time.startTime;
		this.tableParam.endTime = time.endTime;
	} 

	replyId: any='';
	replyContent :any=''; //回复内容
	appletsCode:any = ''; // 回复的不同小程序的对应的code
	//回复内容
	reply(row,modal){
		this.replyId = row.id;
		this.replyContent = '';
		this.appletsCode = row.appletsCode?row.appletsCode:""
		modal.show();
	}

	itemModalClose(modal){
		modal.hide();
	}

	itemModalSubmit(modal){
		let parameters = {
			id: this.replyId,
			replyContent: this.replyContent,
			appletsCode:this.appletsCode
		}
		this.jsonApiService.http_obj("appointmentRepairCar/addReply.html", parameters).subscribe(data => {
			this.jsonApiService.message(data.message);
			modal.hide()
			this.load();
		})
	}
	showMessage(content,title,modal){
		modal.show();
		this.title = title
		this.content = content
	}
}